package concurrent.lock.locksupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * park阻塞线程，unpark解除阻塞线程
 * park/unpark无视顺序，park在unpark之后，也不会被阻塞
 * park/unpark无需在同步代码块中
 */
public class LockSupportDemo {

	public static void main(String[] args) {
		Thread thread1 = new Thread(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "come in");
			//阻塞线程
			LockSupport.park();
			System.out.println(Thread.currentThread().getName() + "唤醒");
		}, "线程1");

		Thread thread2 = new Thread(() -> {
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			LockSupport.unpark(thread1);
			System.out.println(Thread.currentThread().getName() + "通知");
		}, "线程2");

		thread2.start();
		thread1.start();
	}

}
